package org.dbutils.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import org.dbutils.dao.HandlerDao;
import org.dbutils.entity.Staffs;
import org.dbutils.util.DruidUtil;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author ZJL
 * @date 2025-02-27
 */
public class HandlerDaoImpl implements HandlerDao {
    QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
    @Override
    public Map<String,Object> selectById(int id) {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id = ?";
        try {
            return qr.query(sql, new MapHandler(), id);
        } catch (SQLException e) {
            throw new RuntimeException("查询异常",e);
        }

    }

    @Override
    public List<Map<String, Object>> selectAll() {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs";
        try {
            return qr.query(sql,new MapListHandler());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Staffs selectStaffById(int id) {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id = ?";
        try {
            return qr.query(sql,new BeanHandler<>(Staffs.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Staffs> selectAllStaff() {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs";
        try {
           return qr.query(sql,new BeanListHandler<>(Staffs.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String selectStaffNameById(int id) {
        String sql = "select name from staffs where id = ?";
        try {
            return qr.query(sql,new ScalarHandler<>(), id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Long StaffsCount() {
        String sql = "select count(*) from staffs";
        try {
            return qr.query(sql,new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> selectAllStaffName() {
        String sql = "select name from staffs";
        try {
            return qr.query(sql,new ColumnListHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Object[] selectByStaffById(int id) {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id = ?";
        try {
          return qr.query(sql,new ArrayHandler(),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Object[]> selectAllByStaff() {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs";
        try {
            return qr.query(sql,new ArrayListHandler());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
